# mtp_phone_connect
# Builds from https://github.com/puppylinux-woof-CE/petbuilds

#. ../func
. ../build.conf

#URL= the shell source is included in templeate
PKG=mtp_phone_connect
TODAY=`date +%y%m%d`
VER=$TODAY
STATUS_URL=http://distro.ibiblio.org/puppylinux/sources/m/
STATUS_PKG=mtpstatus
STATUS_VER=0.5
COMP=tar.gz
GO_URL=github.com/hanwen/go-mtpfs
GO_PKG=
GO_EXE=go-mtpfs
DESC="Connect Android phones and tablets over usb"
DEPS=+gtk+2,+fuse
CAT=Filesystem
BUILD=
CWD=$(pwd)
[ -z "$MWD" ] && MWD=$(dirname $CWD)	
[ -z "$BUILD" ] && BUILD=$DEF_BUILD
#[ -f "$DESKTOP" ] || DESKTOP=not
#[ -z "$DEPS" ] && DEPS=not	

ARCH=$(uname -m)
case $ARCH in
 *64) 	LIBDIR=$LIBCONF64 ;;
 arm*)	LIBDIR=$LIBCONFarm;;
 *) 	LIBDIR=$LIBCONF32 ;;
esac

retrieve_custom() {
	if [ -f "../0sources/${STATUS_PKG}-${STATUS_VER}.${COMP}" ];then
		cp -a ../0sources/${STATUS_PKG}-${STATUS_VER}.${COMP} .
	else
		wget -t0 -c ${STATUS_URL}/${STATUS_PKG}-${STATUS_VER}.${COMP}
		[ "$?" -eq 0 ] || exit 1
	fi
	
	
	if [ "$DLD_ONLY" -eq 0 ];then
		mv -f ${STATUS_PKG}-${STATUS_VER}.${COMP} ../0sources
		
		exit 0
	fi	
}

extract_custom() {
	tar -xvf ${STATUS_PKG}-${STATUS_VER}.${COMP}
	[ "$?" -eq 0 ] || exit 1
	mv ${STATUS_PKG}-${STATUS_VER}.${COMP} ../0sources
	
}

build() {
	#status icon
	cp -arf $PKG-template  ${PKG}-install
	# since not X compiling forget build triplet
	cd ${STATUS_PKG}-${STATUS_VER}
	make
	[ "$?" -eq 0 ] || exit 1
	strip --strip-unneeded ${STATUS_PKG} 
	install -d -m 0755 $CWD/${PKG}-install/usr/bin/
	install -m 0755 ${STATUS_PKG} $CWD/${PKG}-install/usr/bin/
	cd -
	rm -r ${STATUS_PKG}-${STATUS_VER}
	
	#go-mtpfs from github
	echo
	echo "getting ${GO_EXE} source and compiling.."
	TMP=$CWD/go
	mkdir -p $TMP
	export GOPATH=$TMP
	#this fails and have to batch usb.go after. On the fly is unreliable
	go get -gccgoflags '-static-libgo' $GO_URL || echo keep going
	patch -p0 < go-mtpfs_usb-go.patch
	#try again
	go get -gccgoflags '-static-libgo' $GO_URL
	if [ -x "${TMP}/bin/${GO_EXE}" ];then
		strip --strip-unneeded ${TMP}/bin/${GO_EXE}
		install -m 0755 ${TMP}/bin/${GO_EXE} $CWD/${PKG}-install/usr/bin/
	else exit 1
	fi
	echo "Success compiling ${GO_EXE}"
	#cleanup
	#rm -rf $TMP ##################################################################
	mv ${PKG}-install ${PKG}-${VER}-${ARCH}_${BUILD}
}
	
package() {
	# add this recipe
	install -d -m 0755 ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/src/dev
	cat ${PKG}.petbuild > ${PKG}_DEV-${VER}-${ARCH}_${BUILD}/usr/src/dev/$PKG-build-recipe
	if [ "$DESKTOP" ];then
		install -d -m 0755 ${PKG}-${VER}-${ARCH}_${BUILD}/usr/share/applications
		cat $DESKTOP > ${PKG}-${VER}-${ARCH}_${BUILD}/usr/share/applications/$DESKTOP
	fi
	# split
	#get_files ${PKG}-install ${PKG}-${VER}-${ARCH}_${BUILD}
	#(cd ${PKG}-${VER}; $MWD/split.sh ../${PKG}-install $BUILD)
	# delete any icon cache or library cache
	find ./${PKG}-${VER}-${ARCH}_${BUILD} -type f -name '*cache' -delete
	[ -f ./pinstall.sh ] && install -m 0755 pinstall.sh ./${PKG}-${VER}-${ARCH}_${BUILD}/
	[ -f ./puninstall.sh ] && install -m 0755 puninstall.sh ./${PKG}-${VER}-${ARCH}_${BUILD}/
	for p in `ls|grep "\-${ARCH}"|grep -v "files$"` ; do
		case $p in
			*_DEV*) DESC="$PKG development"; DEPS=+${PKG} ;;
			*_DOC*) DESC="$PKG documentation"; DEPS=+${PKG} ;;
			*_NLS*) DESC="$PKG locales"; DEPS=+${PKG} ;;
		esac	
		echo "packaging $p"	
		dir2pet -x -s -w="$DESC" -d="$DEPS" -c=$CAT -p=${p} 2>&1 >/dev/null
		rm -r $p
		mv ${p}.pet ../0pets_out
	done
	#rm *.files 2>/dev/null
	echo "done!"
}

# main
retrieve_custom
extract_custom
build
package	
